package com.yjt.javatest.controller;

import com.yjt.javatest.entity.QuestionTestLib;
import com.yjt.javatest.entity.TestLib;
import com.yjt.javatest.service.TestLibService;
import com.yjt.javatest.utils.APIResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author yr
 */
@RestController
@Tag(name = "试卷库")
@RequestMapping("/testLib")
@PreAuthorize("@ex.isTeacher()")
@SecurityRequirement(name = "token")
public class TestLibController {
    @Resource
    TestLibService testLibService;
    @GetMapping("/{id}")
    @Operation(summary = "根据id获取试卷模板")
    public APIResponse getTestLibById(@PathVariable Long id) {

        return testLibService.getTestLibById(id);
    }

    @GetMapping()
    @Operation(summary = "获取教师所有试卷模板")
    public APIResponse getTestLibsByUserId() {
        return testLibService.getTestLibsByUserId();
    }
    @PostMapping("/create")
    @Operation(summary = "创建试卷模板")
    public APIResponse createTestLib(@RequestBody TestLib testLib) {
        return testLibService.createTestLib(testLib);
    }

    @PostMapping("/update")
    @Operation(summary = "修改试卷模板")
    public APIResponse updateTestLib(@RequestBody TestLib testLib) {
        return testLibService.updateTestLib(testLib);
    }
    @PostMapping("/question/add")
    @Operation(summary = "向模板中添加题目", description = "只能向模板中添加自己的题目, 不传order默认加到末尾,order不一定连续，但一定不相等")
    public APIResponse addQuestionToTestLib(@RequestBody QuestionTestLib questionInfo) {
        return testLibService.addQuestionToTestLib(questionInfo);
    }
    @PostMapping("/question/switch")
    @Operation(summary = "题目位置上下移动", description = "dir = 0 向上移动， 1 向下移动一个")
    public APIResponse moveQuestionOrder(@RequestParam Long testLibId, @RequestParam Long questionId, @PathVariable Integer dir) {
        return testLibService.moveQuestionOrder(testLibId, questionId, dir);
    }

    @PostMapping("/question/delete")
    @Operation(summary = "删除模板中的题目")
    public APIResponse deleteQuestionFromTestLib(@RequestParam Long questionId, @RequestParam Long testLibId) {
        return testLibService.deleteQuestionFromTestLib(questionId, testLibId);
    }
    @PostMapping("/delete")
    @Operation(summary = "删除试卷模板")
    public APIResponse deleteTestLib(@RequestParam Long testLibId) {
        return testLibService.deleteTestLib(testLibId);
    }
    @GetMapping("/questionId")
    @Operation(summary = "获取模板中的所有题目的id(已排序)", description = "从模板创建测试用这个接口获取题目信息")
    public APIResponse getQuestionIdsByTestLibId(@RequestParam Long testLibId) {
        return testLibService.getQuestionIdsByTestLibId(testLibId);
    }
    @GetMapping("/question")
    @Operation(summary = "获取模板中的所有题目")
    public APIResponse getQuestionListByTestLibId(@RequestParam Long testLibId) {
        return testLibService.getQuestionListByTestLibId(testLibId);
    }
}
